package com.tianji.learning.mapper;

import com.tianji.learning.domain.po.PointsRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tianji.learning.domain.vo.PointsStatisticsVO;
import com.tianji.learning.enums.PointsRecordType;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学习积分记录，每个月底清零 Mapper 接口
 * </p>
 *
 * @author hzy
 * @since 2023-09-13
 */
public interface PointsRecordMapper extends BaseMapper<PointsRecord> {

    /**
     * 查询单日该类型分数值
     *
     * @param userId
     * @param type
     * @param beginTime
     * @param endTime
     */
    @Select("select ifnull(sum(points), 0) from points_record where user_id = #{userId} and type = #{type}" +
            " and create_time >= #{beginTime} and create_time <= #{endTime}")
    Integer selectScore(@Param("userId") Long userId, @Param("type") PointsRecordType type,
                        @Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 分类查询数据库中各类型的积分情况
     *
     * @param userId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select type,ifnull(sum(points),0) as points from points_record where user_id = #{userId} and create_time >= #{beginTime} " +
            "and create_time <= #{endTime} group by type")
    List<PointsRecord> selectAllScore(@Param("userId") Long userId, @Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);
}
